package com.jinyou.chengfengshop.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.jinyou.chengfengshop.Constant.AppUrl;
import com.jinyou.chengfengshop.OkHttpUtils.CallBackUtil;
import com.jinyou.chengfengshop.OkHttpUtils.OkhttpUtil;
import com.jinyou.chengfengshop.R;
//import com.wangyou.chengfengseller.adapter.AddressAdapter;
import com.jinyou.chengfengshop.adapter.AddressAdapter;
import com.jinyou.chengfengshop.bean.LineBean;
import com.jinyou.chengfengshop.bean.addresBean;
import com.jinyou.chengfengshop.bean.infosBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import okhttp3.Call;

/**
 * 发单
 * Created by zhuzhiqiang on 2018/9/23.
 */

public class AddOrderActivity extends AppCompatActivity {
    private List<LineBean> adb = new ArrayList<LineBean>();
    private List<infosBean> ifb = new ArrayList<infosBean>();
    private List<addresBean> adBean = new ArrayList<addresBean>();
    private AddressAdapter mAdapter;
    private GridLayoutManager addManager;
    private String shopRegionId;
    private RecyclerView addRec;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addorder);
        shopRegionId = getIntent().getStringExtra("shopRegionId");
        addRec = (RecyclerView) findViewById(R.id.add_list);
        GridLayoutManager layoutManage = new GridLayoutManager(this, 2);
        addRec.setLayoutManager(layoutManage);

        getAddresInfo();
    }







    private void getAddresInfo(){
        HashMap<String,String> paramsMap = new HashMap<>();
        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
        String tocken = sh.getString("tocken","");
        paramsMap.put("token",tocken);
        paramsMap.put("shopRegionId",shopRegionId);
        paramsMap.put("sysCustomer","o2o-chengfeng");

        OkhttpUtil.okHttpPost(AppUrl.SHOP_LIST_ADDR, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(AddOrderActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {//请求成功时调用，执行在UI线程
                try {

                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("status");
                    if(code == 1){
                        JSONArray db = obj.getJSONArray("data");

                        for(int m=0;m<db.length();m++){
                            //List infosBean = new ArrayList();
                            LineBean lb = new LineBean();

                            JSONObject om = (JSONObject) db.get(m);
                            lb.setName(om.getString("name"));
                            lb.setNameSn(om.getString("nameSn"));
                            JSONArray infos = om.getJSONArray("infos");
                            for(int j=0;j<infos.length();j++){
                                JSONObject oj = (JSONObject) infos.get(j);
                                infosBean ibj = new infosBean();
                                ibj.setId(oj.getLong("id"));
                                ibj.setRoute(oj.getString("route"));
                                ibj.setRouteAddr(oj.getString("routeAddr"));
                                ibj.setStationId(oj.getString("stationId"));
                                ibj.setStationName(oj.getString("stationName"));
                                ibj.setNote(oj.getString("psMoney"));

                                JSONArray addres = oj.getJSONArray("addres");
                                for(int k = 0;k<addres.length();k++){
                                    JSONObject ok = (JSONObject) addres.get(k);
                                    addresBean ab = new addresBean();
                                    ab.setId(ok.getLong("id"));
                                    ab.setAddr(ok.getString("routeId"));
                                    ab.setAddrSn(ok.getString("addrSn"));
                                    adBean.add(ab);
                                }
                                ibj.setAdb(adBean);
                                ifb.add(ibj);
                            }
                            lb.setInfob(ifb);


                            adb.add(lb);


                        }


                        mAdapter = new AddressAdapter(AddOrderActivity.this,adb);
                        addRec.setAdapter(mAdapter);


                    }else {
                        Toast.makeText(AddOrderActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                        reLogin();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }


    //重新登陆
    private void reLogin(){
        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor edt = sh.edit();
        edt.putString("tocken","");
        edt.putString("info","");
        edt.commit();
        Intent mIntent = new Intent(AddOrderActivity.this,LoginActivity.class);
        startActivity(mIntent);
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }


    //    public void finishAddOrder(){
//        this.finish();
//    }
}
